import { createStore } from "vuex"
import {Itab} from "./type";

interface State{
    tabsList: Array<Itab>
    contextMenuTadId: string
    editBlogId: number
    token: string
}

export default createStore<State>({
    state: {
        tabsList: [],
        contextMenuTadId: '',
        editBlogId: 0,
        token: '',
    },
    mutations: {
        // 添加tab
        addTab(state:State,tab:Itab){
            const isSome = state.tabsList.some((item)=> item.path == tab.path)
            if(!isSome){
                state.tabsList.push(tab);
            }
        },

        addToken(state:State,token:string){
            state.token = token;
        },



        // 删除 tab
        closeCurrentTab(state: State,targetName: string){
            const index = state.tabsList.findIndex((item)=> item.path === targetName)
            state.tabsList.splice(index,1);
        },

        // 右键打开菜单保存PATH
        saveCurContextTabId(state: State,curMenuTadId: string){
            state.contextMenuTadId = curMenuTadId;
        },

        // 删除所有标签页
        closeAllTabs(state:State){
            state.tabsList = []
            sessionStorage.removeItem('TABS_ROUTES')
        },

        // 删除其他标签页
        closeOtherTabs(state:State,par: string){
            if(par == 'other'){
                state.tabsList = state.tabsList.filter((item)=>item.path == state.contextMenuTadId)
            }else if(par == 'left'){
                const index = state.tabsList.findIndex((item)=>item.path == state.contextMenuTadId)
                state.tabsList.splice(0,index);
            }else{
                const index = state.tabsList.findIndex((item)=>item.path == state.contextMenuTadId)
                state.tabsList.splice(index + 1);
            }
        },

        addEditBlogId(state: State,blogId: number){
            state.editBlogId = blogId
        },

        removeEditBlogId(state: State){
            state.editBlogId = 0
        }
    },
    getters: {
        getAddTab(state: State){
            return state.tabsList
        },

        getEditBlogId(state: State){
            return state.editBlogId
        },
        getToken(state:State){
            return state.token
        },
    },
    actions: {

    },
    modules: {
    }
})
